package gui.panel_new.evaluation.professor;

import enums.FileFilterEnum;
import enums.URLEnum;
import gui.panel_new.OpenSavePanel;
import j_util.Utiles;
import model.eval.Test;

import javax.swing.*;
import java.awt.event.ActionEvent;
/*

    This file is part of Logica-Logike.

    Logica-Logike is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation version 3 of the License.

    Logica-Logike is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Logica-Logike.  If not, see <http://www.gnu.org/licenses/>.

 */
/**
 * Permite visualizar un test;
 *
 * @author Jose Martin Rozanec;
 */
public class TestOverviewPanel extends OpenSavePanel {
    private Test test;
    private JScrollPane scrollPane;
    private JTextArea previewTextArea;

    /**
     * Constructor;
     *
     * @param saveButton;
     * @param openButton;
     * @param fileChooser;
     */
    public TestOverviewPanel(JButton saveButton, JButton openButton, JFileChooser fileChooser) {
        this.saveButton = saveButton;
        this.openButton = openButton;
        this.fileChooser = fileChooser;
        fileFilter = FileFilterEnum.TST;
        previewTextArea = new JTextArea(39, 60);
        previewTextArea.setEditable(false);
        scrollPane = new JScrollPane(previewTextArea);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        add(scrollPane);
    }

    protected void setSaveActionListener() {
        class SaveTestAction extends AbstractAction {
            public SaveTestAction() {
                super("", Utiles.createImageIcon(URLEnum.getReference(URLEnum.SAVE_AS)));
            }

            public void actionPerformed(ActionEvent e) {
                String path = "";
                int state = fileChooser.showSaveDialog(JOptionPane.getRootFrame());
                if (state == JFileChooser.APPROVE_OPTION) {
                    path = Utiles.getFilePathWithExtension(fileChooser.getSelectedFile().getPath(), ".tst");
                    Utiles.serialize(test, path);
                }
            }
        }
        saveButton.setAction(new SaveTestAction());
    }

    protected void setSaveButtonEnabled() {
        saveButton.setEnabled(true);
    }

    protected void setOpenActionListener() {
        //remains unimplemented
    }

    protected void setOpenButtonEnabled() {
        openButton.setEnabled(false);
    }

    protected void setFocusOnComponent() {
        //remains unimplemented...
    }

    /**
     * Permite setear el test a ser visualizado por el usuario;
     *
     * @param test;
     */
    public void setTest(Test test) {
        this.test = test;
        previewTextArea.setText(test.toString());
    }
}
